package com.example.xcService.pojo.tool;

import lombok.extern.slf4j.Slf4j;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.Random;

@Slf4j
public class StringTool {
    public static String GetRandomText(int len)
    {
        String txtChars = "QWERTYUIOPLKJHGFDSAZXCVBNMmnbvcxzlkjhgfdsaqwertyuiop2346789";
        StringBuilder sb = new StringBuilder(len);

        Random random = new Random(1);
        for (int n = 0; n <= len - 1; n++){
            int maxLength = txtChars.length();
            int index=random.nextInt(maxLength);
            sb.append(txtChars.substring(index, index+1));
        }
        return sb.toString();
    }

    public static String stringToMD5(String plainText) {
        byte[] secretBytes = null;
        StringBuilder md5code = new StringBuilder();
        try {
            secretBytes = MessageDigest.getInstance("md5").digest(
                    plainText.getBytes("UTF-8"));
            md5code = new StringBuilder(new BigInteger(1, secretBytes).toString(16));
            while (md5code.length() < 32) {
                md5code.insert(0, "0");
            }
        } catch (Exception ex) {
            log.error("md5加密异常：" + ex.getMessage(), ex);
        }

        return md5code.toString();
    }
}
